package net.xfok.tool.wsy;

import java.text.DecimalFormat;
import java.util.Random;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Main extends Activity implements OnItemSelectedListener {
	
	private static final String TAG="Main";
	private static final Random RNG = new Random();
	private static final int SPEED=5000;
	private static final int ANSWER_LEN=5;
	
	private static final int MENU_ORDER=Menu.FIRST;
	private static final int MENU_RANDOM=Menu.FIRST+1;
	private static final int MENU_TEST=Menu.FIRST+2;
	
	private static final int MODE_ORDER=0;
	private static final int MODE_RANDOM=1;	
	private static final int MODE_TEST=2;
	
	private int mode=MODE_ORDER;
	private int id=0;
	private int right=0;
	private int wrong=0;
	private boolean startTest=false;
	private String[] anwsers=new String[ANSWER_LEN];
	
	private TextView tv,ask,stat,lm,ls;
	private Spinner anwser;
	private ArrayAdapter<String> adapter;
	private Resources res;
	private String memo;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        res=getResources();
        tv=(TextView)findViewById(R.id.text);
        lm=(TextView)findViewById(R.id.label_memo);
        stat=(TextView)findViewById(R.id.label_stat);
        ask=(TextView)findViewById(R.id.ask);
        ls=(TextView)findViewById(R.id.label_select);
        anwser=(Spinner)findViewById(R.id.anwser);
        anwser.setOnItemSelectedListener(this);
        adapter=new ArrayAdapter<String>(this,
        		android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        refresh();
    }
    
    @Override
    public void onConfigurationChanged(Configuration config) {
        try {
            super.onConfigurationChanged(config);
            if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                
            }
        } catch (Exception ex) {
        }
    }
    
    Handler myHandler = new Handler() {  
        public void handleMessage(Message msg) {      	
        	refresh();
            super.handleMessage(msg);   
        }   
    };  
    
    private void refresh(){	
    	Message message = new Message();   
    	message.what = 0;   
    	switch (mode) {
			case MODE_RANDOM:		
				anwser.setVisibility(View.GONE);
				ask.setVisibility(View.GONE);
				stat.setVisibility(View.GONE);
				ls.setVisibility(View.GONE);
				tv.setText(Constant.PING[id]);
				memo=String.format(res.getString(R.string.label_memo), Constant.YIN[id],Constant.PIAN[id]);
				lm.setText(memo);
				tv.setVisibility(View.VISIBLE);
				lm.setVisibility(View.VISIBLE);
				id=RNG.nextInt(Constant.PING.length);
				Main.this.myHandler.sendMessageDelayed(message,SPEED); 
				break;
			case MODE_ORDER:
				tv.setVisibility(View.VISIBLE);
				anwser.setVisibility(View.GONE);
				ask.setVisibility(View.GONE);
				stat.setVisibility(View.GONE);
				ls.setVisibility(View.GONE);
				tv.setText(Constant.PING[id]);
				memo=String.format(res.getString(R.string.label_memo), Constant.YIN[id],Constant.PIAN[id]);
				lm.setText(memo);
				tv.setVisibility(View.VISIBLE);
				lm.setVisibility(View.VISIBLE);
				id=(id+1)%Constant.PING.length;
				Main.this.myHandler.sendMessageDelayed(message,SPEED); 
				break;
			case MODE_TEST:
				id=RNG.nextInt(Constant.PING.length);
				tv.setVisibility(View.GONE);
				lm.setVisibility(View.GONE);
				ask.setVisibility(View.VISIBLE);
				ask.setText(Constant.PING[id]);
				stat.setVisibility(View.VISIBLE);
				ls.setVisibility(View.VISIBLE);
				anwser.setVisibility(View.VISIBLE);
				String stats=res.getString(R.string.label_stat);
				float rat=(float)right/(float)((right+wrong)==0?1:(right+wrong));				
				stat.setText(String.format(stats, right,wrong,new DecimalFormat(".##").format(rat*100)+"%"));
				setAnwsers();		        
		        anwser.setAdapter(adapter);
			default:
				break;
		}  		
    }
    
    public boolean onCreateOptionsMenu(Menu menu) { 
    	menu.add(0, MENU_ORDER, 0, res.getString(R.string.menu_order)).setIcon(android.R.drawable.ic_menu_view);
    	menu.add(0, MENU_RANDOM, 0, res.getString(R.string.menu_random)).setIcon(android.R.drawable.ic_menu_more);
    	menu.add(0, MENU_TEST, 0, res.getString(R.string.menu_test)).setIcon(android.R.drawable.ic_menu_help);
    	return true;
    }
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	Main.this.myHandler.removeMessages(0);
    	switch (item.getItemId()) {
			case MENU_ORDER:
				id=0;
				mode=MODE_ORDER;		
				break;
			case MENU_RANDOM:
				mode=MODE_RANDOM;
				break;
			case MENU_TEST:
				mode=MODE_TEST;				
				break;
			default:
				break;
		}
    	refresh();
    	return true;
    }
	
	private void setAnwsers(){	
		adapter.clear();
		startTest=false;
		int rightAnwser=RNG.nextInt(ANSWER_LEN);
		rightAnwser=rightAnwser==0?rightAnwser+1:rightAnwser;
		anwsers[rightAnwser]=Constant.YIN[id];
		anwsers[0]=String.format(res.getString(R.string.select_pronounce), "["+Constant.PING[id]+"]");
		for(int i=0;i<ANSWER_LEN;i++){
			if(i>0&&i!=rightAnwser){
				anwsers[i]=Constant.YIN[RNG.nextInt(Constant.YIN.length)];				
			}
			adapter.add(anwsers[i]);
		}	
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long itemId) {
		// TODO Auto-generated method stub
		if(startTest){
			Log.i(TAG, "select:"+position+" "+anwsers[position].equals(Constant.YIN[id]));
			if(anwsers[position].equals(Constant.YIN[id])){
				right++;
			}else{
				wrong++;
			}
			refresh();
		}else{
			startTest=true;
		}
	}

	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}
}